﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.SingleWell.Services;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Models;
using Web.SystemConfig.SysCache;
using Web.SystemConfig.WorkFlow;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Business;
using Web.SystemConfig.Models;
using Web.SystemConfig.Services;
using Web.SystemConfig.Business;
using Web.SystemLogin.Models;
using System.Data;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellExame : PageBase
    {
        [Dependency]
        public ICodeTable CodeTableService
        {
            get;
            set;
        }
        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IAttachmentService m_IAttachmentService
        {
            get;
            set;
        }

        private User currentUser
        {
            get
            {
                return (User)Session["CurrentUser"];
            }
        }

        private int m_CurrentStaus
        {
            get
            {
                if (ViewState["CurrentStatus"] != null)
                {
                    return int.Parse(ViewState["CurrentStatus"].ToString());
                }
                else
                {
                    throw new Exception("获取当前数据的状态失败");
                }
            }
            set
            {
                ViewState["CurrentStatus"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["RecordID"] == null)
                    {
                        throw new Exception("参数RecordID不能为空");
                    }
                    string RecordID = Request.QueryString["RecordID"];

                    this.BindSingleData(RecordID);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                Response.End();
            }
        }

        private void BindSingleData(string wellDataID)
        {
            SingleWellModel oModel = this.m_ISingleWellService.GetSingleWellModel(wellDataID);
            if (oModel == null)
            {
                return;
            }
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
            ///判断数据权限，看是否拥有数据浏览和数据操作权限
            WorkFlow oWorkFlow = new WorkFlow();
            bool canEdit = false;
            bool canView = false;
            bool canOperate = false;
            ///先签收数据
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = new Guid(wellDataID);
            oWorkFlowModel.FromStatusID = (int)singleRow.STATUSID;
            oWorkFlow.SignData(oWorkFlowModel);
            WriteUserLog(string.Format("用户{0}签收代办ID:{1}的单井数据", currentUser.Real_Name, wellDataID), OracleLogType.SingleWell);

            string msg = oWorkFlow.GetPrivilege(ArchiveTableEnum.SingleWell, currentUser.User_ID, singleRow.DATAID, ref canEdit, ref canView, ref canOperate);
            if (!canView)
            {
                throw new Exception("您没有权限浏览此数据");
            }
            if (canOperate)
            {
                this.btnAgree.Visible = true;
                this.btnDisAgree.Visible = true;
            }

            //绑定数据
            if (this.BindSingleData(oModel) && this.BindWellDetail(oModel) && this.BindAttachment(oModel))
            {
                this.m_CurrentStaus = int.Parse(oModel.SINGLEWELL[0].STATUSID.ToString());
            }
        }

        #region 绑定数据
        private bool BindAttachment(SingleWellModel oModel)
        {
            try
            {
                DataTable oDataTable = null;
                this.m_IAttachmentService.Initialize("SingleWell");
                SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
                if (singleRow == null)
                {
                    return true;
                }
                List<AttachmentModel> modelList = this.m_IAttachmentService.GetAttachmentModelByObjectID(singleRow.DATAID);
                if (modelList != null && modelList.Count > 0)
                {
                    oDataTable = new DataTable();
                    oDataTable.Columns.Add("attachmentid");
                    oDataTable.Columns.Add("attachmentname");
                    oDataTable.Columns.Add("uploadtime");
                    oDataTable.Columns.Add("attachmentsize");

                    foreach (AttachmentModel model in modelList)
                    {
                        DataRow dr = oDataTable.NewRow();
                        dr["attachmentid"] = model.AttachmentID;
                        dr["attachmentname"] = model.AttachmentName;
                        dr["uploadtime"] = model.UpdateTime;
                        dr["attachmentsize"] = model.AttachmentSize;

                        oDataTable.Rows.Add(dr);
                    }
                }
                if (oDataTable != null && oDataTable.Rows.Count > 0)
                {
                    this.gv_AttachMent.DataSource = oDataTable;
                    this.gv_AttachMent.DataBind();
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool BindWellDetail(SingleWellModel oModel)
        {
            try
            {
                DataTable oDataTable = oModel.WELLDATADETAIL as DataTable;
                this.gv_SingleWellDetail.DataSource = oDataTable;
                this.gv_SingleWellDetail.DataBind();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool BindSingleData(SingleWellModel oModel)
        {
            try
            {
                SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
                if (!singleRow.IsA0Null())
                {
                    this.txtA0.Text = singleRow.A0.ToString();
                }
                if (!singleRow.IsA3Null())
                {
                    this.txtA3.Text = singleRow.A3.ToString();
                }
                if (!singleRow.IsA4Null())
                {
                    this.txtA4.Text = singleRow.A4.ToString();
                }
                if (!singleRow.IsBEGINDATENull())
                {
                    this.txtBEGINDATE.Text = singleRow.BEGINDATE.ToShortDateString();
                }
                if (!singleRow.IsBEGINWORKDATENull())
                {
                    this.txtBeginWorkDate.Text = singleRow.BEGINWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHDATENull())
                {
                    this.txtFINISHDATE.Text = singleRow.FINISHDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHWORKDATENull())
                {
                    this.txtFinishWorkDate.Text = singleRow.FINISHWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsWELLDEPTHNull())
                {
                    this.txtWellDepth.Text = singleRow.WELLDEPTH.ToString();
                }
                if (!singleRow.IsDATAFILENAMENull())
                {
                    this.txtDatafileName.Text = singleRow.DATAFILENAME;
                }
                if (!singleRow.IsSTATUSIDNull())
                {
                    this.hdfCurrentStatusID.Value = singleRow.STATUSID.ToString();//赋值当前状态
                }
                if (!singleRow.IsWELLTYPENull())
                {
                    this.txtWellType.Text = singleRow.WELLTYPE;
                }
                if (!singleRow.IsWORKMODENull())
                {
                    this.txtWorkMode.Text = singleRow.WORKMODE;
                }

                this.txtWellCode.Text = singleRow.WELLCODE;
                      /*****************************************
                 * 
                 * add by guoyin 2014-3-26
                 * 增加了井类别和类别明细
                 * 
                 *****************************************/
                if (!singleRow.IsCATEGORYNull())
                {

                    this.txtCategory.Text = CodeTableService.GetpkNameByCodepkValue(singleRow.CATEGORY); ;
                }
                if (!singleRow.IsCATEGORYDETAILNull())
                {
                    this.txtCateGoryDetail.Text = CodeTableService.GetpkNameByCodepkValue(singleRow.CATEGORYDETAIL); ;
                }
                /*END**/
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 按钮事件
        protected void btnAgree_Click(object sender, EventArgs e)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.CommentMessage = this.txtCommsg.Text.Trim();
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = new Guid(Request.QueryString["RecordID"]);
            oWorkFlowModel.FromStatusID = this.m_CurrentStaus;
            WorkFlowMessageStatusRule CurrentRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(this.m_CurrentStaus, "同意");
            oWorkFlowModel.OperateID = CurrentRule.OperateID;
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            oWorkFlow.FlowProcess(oWorkFlowModel);
            MyAlert("审核数据成功", "SingleWellExameView.aspx?jobtype=1");
            WriteUserLog(string.Format("用户{0}审核了ID:{1}的单井数据", currentUser.Real_Name, oWorkFlowModel.RecordID), OracleLogType.SingleWell);
        }

        protected void btnDisAgree_Click(object sender, EventArgs e)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.CommentMessage = this.txtCommsg.Text.Trim();
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = new Guid(Request.QueryString["RecordID"]);
            oWorkFlowModel.FromStatusID = this.m_CurrentStaus;
            WorkFlowMessageStatusRule CurrentRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(this.m_CurrentStaus, "不同意");
            oWorkFlowModel.OperateID = CurrentRule.OperateID;
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            oWorkFlow.FlowProcess(oWorkFlowModel);
            MyAlert("审核数据成功", "SingleWellExameView.aspx?jobtype=1");
            WriteUserLog(string.Format("用户{0}审核了ID:{1}的单井数据", currentUser.Real_Name, oWorkFlowModel.RecordID), OracleLogType.SingleWell);
        }
        #endregion
    }
}